package com.lx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.lx.entity.Resume;
import com.lx.utils.DBUtils;

public class ApplicantDao {
	
	public int login(String email,String password){
		
		String sql = "select applicant_id from TB_APPLICANT where APPLICANT_EMAIL=? and APPLICANT_PWD=?";		
		int id=0;
		try{
			Connection conn = DBUtils.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, password);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				id = rs.getInt(1) ;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return id;
	}
	
	public Boolean isExitEmail(String email){
		
		String sql = "select * from TB_APPLICANT where APPLICANT_EMAIL=?";		
		
		Connection conn =null;
		ResultSet rs =null;
		PreparedStatement ps = null;
		
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			rs = ps.executeQuery();
			if(rs.next()){
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, rs, null, ps);
		}
		return true;
	}
	
	public void saveEmail(String email,String password){
		
		Connection conn =null;
		ResultSet rs =null;
		PreparedStatement ps = null;
		String sql = "insert into TB_APPLICANT (`APPLICANT_EMAIL`, `APPLICANT_PWD`, `APPLICANT_REGISTDATE`) VALUES (?,?,?)";
//		String sql1 = "insert into tb_RESUME_BASICINFO (`APPLICANT_ID`) VALUES (?)";
//		String sql2 = "select APPLICANT_ID from TB_APPLICANT where APPLICANT_EMAIL=?";
		try{
			conn = DBUtils.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, password);
			ps.setTimestamp(3,new Timestamp(new Date().getTime()));
			ps.executeUpdate();
			
//			ps = conn.prepareStatement(sql2);
//			ps.setString(1, email);
//			rs = ps.executeQuery();
//			String id = null;
//			while(rs.next()){
//				id = rs.getString("APPLICANT_ID");
//			}
//			
//			ps = conn.prepareStatement(sql1);
//			ps.setString(1, id);
//			ps.executeUpdate();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, rs, null, ps);
		}
	}
	
}
